/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
/* eslint-disable eqeqeq */
/* eslint-disable no-redeclare */
/* eslint-disable no-eval */
function WordCheck(inputStr, rightStr) {
  Result = []
  MaxMatch = 0
  RightStr = rightStr
  InputStr = inputStr

  this.Check = function(str1, str2, pos1, pos2) {
    var index1 = pos1
    var index2 = pos2
    if (index1 < str1.length && index2 < str2.length) {
      if (str2.length > 10 && GetCharCount(str1, '#$') >= str2.length / 2) {
        return
      }
      if (index1 + 1 < str1.length) {
        var posx = str1.substring(index1 + 1).indexOf(str2[index2])
        if (posx >= 0) {
          var str1Temp1 = str1
          var i = 0
          var replaceChars = ''
          while (i <= posx) {
            replaceChars += '$'
            i++
          }
          str1Temp1 = ReplaceStr(str1Temp1, index1, index1 + posx + 1, replaceChars)

          this.Check(str1Temp1, str2, index1 + posx + 1, index2)
        }
      }

      if (index2 + 1 < str2.length) {
        var posx = str2.substring(index2 + 1).indexOf(str1[index1])
        if (posx >= 0) {
          var str1Temp1 = str1
          var i = 0
          var insertChars = ''
          while (i <= posx) {
            insertChars += '#'
            i++
          }

          str1Temp1 = InsertStr(str1Temp1, index1, insertChars)
          this.Check(str1Temp1, str2, index1 + posx + 1, index2 + posx + 1)
        }
      }

      if (str1[index1] !== str2[index2]) {
        var str1Temp1 = str1
        str1Temp1 = ReplaceStr(str1Temp1, index1, index1 + 1, '$')
        this.Check(str1Temp1, str2, index1 + 1, index2 + 1)
      } else {
        var str2Temp1 = str2
        str2Temp1 = ReplaceStr(str2Temp1, index2, index2 + 1, '#')
        this.Check(str1, str2Temp1, index1 + 1, index2 + 1)
      }
    }

    if (index1 == str1.length || index2 == str2.length) {
      var str1Temp1 = str1
      if (index1 < str1.length) {
        var replaceChars = ''
        for (var index = index1; index < str1.length; index++) {
          replaceChars += '$'
        }
        str1Temp1 = ReplaceStr(str1Temp1, index1, str1.length, replaceChars)
      } else if (index2 < str2.length) {
        for (var index = index2; index < str2.length; index++) {
          str1Temp1 += '#'
        }
      }

      var notMatchCount = GetCharCount(str1Temp1, '^#$')
      if (notMatchCount >= MaxMatch) {
        CheckExistForAdd(Result, notMatchCount, str1Temp1, str2)
        MaxMatch = notMatchCount
      }
    }
  }

  this.SelectTheBest = function() {
    var bestStr = [InputStr, RightStr]
    if (Result.length > 0) {
      var item = GetItemByKey(Result, MaxMatch)
      if (item != null) {
        var maxCount = 10000
        for (var i = 0; i < item.value.length; i++) {
          var str = item.value[i].v1.replace(/[A-Za-z0-9_]{2,}/g, '')
          if (str.length < maxCount) {
            maxCount = str.length
            bestStr = [item.value[i].v1, item.value[i].v2]
          }
        }
      }
    }
    return bestStr
  }

  CheckExistForAdd = function(array, itemKey, itemValue, itemValue1) {
    if (array.length > 0) {
      for (var i = 0; i < array.length; i++) {
        if (array[i].key == itemKey) {
          array[i].value.push({ 'v1': itemValue, 'v2': itemValue1 })
          return true
        }
      }
    }

    var items = []
    items.push({ 'v1': itemValue, 'v2': itemValue1 })
    array.push({ 'key': itemKey, 'value': items })

    return false
  }

  function GetItemByKey(array, key) {
    if (array.length > 0) {
      for (var i = 0; i < array.length; i++) {
        if (array[i].key === key) {
          return array[i]
        }
      }
    }
    return null
  }

  function GetCharCount(str, char) {
    var re = eval('/[' + char + ']/g')
    var m = str.match(re)
    if (m != null) {
      return m.length
    } else {
      return 0
    }
  }

  function InsertStr(soure, start, newStr) {
    return soure.substring(0, start) + newStr + soure.substring(start)
  }

  function ReplaceStr(soure, pos, pos1, newStr) {
    return soure.substring(0, pos) + newStr + soure.substring(pos1)
  }

  this.HighlightInputWord = function(str, inputStr) {
    var temp = str// .replace(/[#]{1,}/g, "#");

    var result = ''

    var originalCharIndex = 0
    for (var i = 0; i < temp.length; i++) {
      if (temp[i] == '#') {
        result += "<t style='color:red!important;background-color: #FFCCCC;'>&nbsp;</t>"
      } else if (temp[i] == '$') {
        result += "<t style='color:red!important;background-color: #FFCCCC;'>" + inputStr[originalCharIndex].replace(/ /g, '&nbsp;') + '</t>'
        originalCharIndex++
      } else {
        result += '<t>' + temp[i].replace(/ /g, '&nbsp;') + '</t>'
        // result += temp[i].replace(/ /g, "&nbsp;");
        originalCharIndex++
      }
    }

    return result
  }

  this.HighlightRightWord = function(str, str1) {
    var wrapStr = ''
    var result = ''
    var pos = 0
    var pos1 = 0

    for (var i = 0; i < str.length; i++) {
      if (str1[i] == '#') {
        wrapStr += str[i]
      } else {
        wrapStr += '#'
      }
    }

    while (pos1 >= 0 && pos < wrapStr.length) {
      pos1 = wrapStr.indexOf('#', pos)
      if (pos1 >= 0) {
        if (pos1 > 0) {
          // result += wrapStr.substring(pos, pos1).replace(/ /g, "&nbsp;");
          result += wrapStr.substring(pos, pos1).replace(/([^\s])/g, '<t>$1</t>').replace(/([\s])/g, '<t>&nbsp;</t>')
        }
        result += "<t style='background-color:#CCFF99;color:green!important'>" + str[pos1].replace(/ /g, '&nbsp;') + '</t>'
        pos = pos1 + 1
      }
    }
    if (pos < wrapStr.length) {
      result += wrapStr.substring(pos, wrapStr.length).replace(/([^\s])/g, '<t>$1</t>').replace(/([\s])/g, '<t>&nbsp;</t>')
      // result += "<t o='1'>" + wrapStr.substring(pos, wrapStr.length).replace(/ /g, "&nbsp;") + "</t>";
      // result += wrapStr.substring(pos, wrapStr.length).replace(/ /g, "&nbsp;");
    }
    return result
  }
}
